package com.maddox.relax.view;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.maddox.relax.R;
import com.maddox.relax.adapter.CommonAdapter;
import com.maddox.relax.model.DataModel;
import com.maddox.relax.view.sub.MSelectFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Maddox
 * 2021-11-30
 */
public class MPopListView extends LinearLayout {

    private Context mContext;

    private MSelectFlowLayout flowLayout;

    private ImageView iv_arrow;

    private List<DataModel> allDatas;


    public MPopListView(Context context) {
        this(context, null);
    }

    public MPopListView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MPopListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.layout_pop_list_view, this, true);
        initView();
    }

    private void initView() {
        allDatas = new ArrayList<>();
        flowLayout = findViewById(R.id.mflowlayout);
        iv_arrow = findViewById(R.id.iv_arrow);
        iv_arrow.setOnClickListener(v -> {
            showPopWindow(this);
        });
        flowLayout.setOnItemClickListener(new MSelectFlowLayout.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                String removeName = flowLayout.removeItem(position);
                for (DataModel allData : allDatas) {
                    if (allData.getText().equals(removeName)) {
                        allData.setSelected(false);
                    }
                }
            }
        });

    }

    /**
     * 外部调用
     * 初始化数据
     * 设置所有待展示的数据
     *
     * @param list 展示的数据
     */
    public void setInitList(List<DataModel> list) {
        allDatas.clear();
        allDatas.addAll(list);
    }

    /**
     * 外部调用
     * 返回已选择的所有数据
     *
     * @return
     */
    public List<DataModel> getSelectedList() {
        List<DataModel> selectedList = new ArrayList<>();
        for (DataModel data : allDatas) {
            if (data.getSelected()) {
                selectedList.add(data);
            }
        }
        return selectedList;
    }


    /**
     * 外部调用
     * 设置展示的子条目文本颜色
     *
     * @param color 颜色值
     */
    public void setItemTextColor(int color) {
        flowLayout.setItemTextColor(color);
    }

    /**
     * 外部调用
     * 设置展示的子条目背景颜色
     *
     * @param drawable 自定义的Drawable类型
     */
    public void setItemBackgroundDrawable(Drawable drawable) {
        flowLayout.setItemBackgroundDrawable(drawable);
    }

    /**
     * 外部调用
     * 设置展示的子条目文本右侧小图标,默认是x
     *
     * @param imgRes 图片资源id
     */
    public void setItemTextIcon(int imgRes) {
        flowLayout.setItemTextIcon(imgRes);
    }

    /**
     * 外部调用
     * 设置调出弹窗的图片
     *
     * @param imgRes 图片资源
     */
    public void setArrowImage(int imgRes) {
        iv_arrow.setImageResource(imgRes);
    }


    private void setSelectDataList(List<DataModel> list) {

        flowLayout.setData(list);
        //更新所有数据中已选中的状态
        for (DataModel model : list) {
            if (allDatas.contains(model)) {
                int index = allDatas.indexOf(model);
                allDatas.get(index).setSelected(model.getSelected());
            }
        }
    }


    private void showPopWindow(MPopListView popListView) {

        View contentView = LayoutInflater.from(mContext).inflate(R.layout.layout_pop_list_view_fly_content, null, false);
        GridView gridView = contentView.findViewById(R.id.fly_grid_view);
        TextView cancleBtn = contentView.findViewById(R.id.tv_dialog_cancle);
        TextView confirmBtn = contentView.findViewById(R.id.tv_dialog_confirm);
        ListItemAdapter itemAdapter = new ListItemAdapter(mContext, allDatas, R.layout.layout_pop_list_view_fly_content_item);
        gridView.setAdapter(itemAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(mContext, allDatas.get(position).getText(), Toast.LENGTH_SHORT).show();
                DataModel item = itemAdapter.getItem(position);
                item.setSelected(!item.getSelected());
                itemAdapter.notifyDataSetChanged();

            }
        });

        AlertDialog alertDialog = new AlertDialog.Builder(mContext)
                .setView(contentView)
                .create();
        cancleBtn.setOnClickListener(v -> {
            if (alertDialog != null && alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
        });

        confirmBtn.setOnClickListener(v -> {
            if (alertDialog != null && alertDialog.isShowing()) {
                alertDialog.dismiss();
                //获取已选择的
                popListView.setSelectDataList(itemAdapter.getSelectedList());

            }
        });

        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        alertDialog.getWindow().setAttributes(lp);
        alertDialog.show();


    }

    private class ListItemAdapter extends CommonAdapter<DataModel> {


        public ListItemAdapter(Context mContext, List<DataModel> datas, int layoutId) {
            super(mContext, datas, layoutId);
        }

        @Override
        public void bindView(ViewHolder holder, DataModel dataModel, int position) {
            holder.setText(R.id.item_text, dataModel.getText());
            CheckBox checkBox = holder.getView(R.id.item_checkbox);
            if (dataModel.getSelected()) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }

        }

        public List<DataModel> getSelectedList() {
            List<DataModel> datas = getDatas();
            List<DataModel> haveChoose = new ArrayList<>();
            for (DataModel data : datas) {
                if (data.getSelected()) {
                    haveChoose.add(data);
                }
            }
            return haveChoose;
        }
    }


}


